import pygame
from pygame.locals import *
from Constant import *

"""
Audio.Manager
------
A class for handeling sound effects and bgm.
"""

class AudioManager:
    muted = False
    sfxVolume = 1.0
    
    #Fire and Forget
    #Standarde Method for firing off Sound Effects
    def fire(self, x):
        if self.muted == False:
            sfx = pygame.mixer.Sound(pathSFX + x)
            sfx.set_volume(self.sfxVolume)
            sfx.play()
    
    #As it sounds, this sets the bgm.
    #If there is something already playing, it'll fadeout for 2 seconds
    #before starting the next clip.
    #Also, if None is passed, it'll fade out and stop the bgm.
    def setBGM(self, x):
        if self.muted == False:
            if x != None:
                if pygame.mixer.music.get_busy() == False:
                    pygame.mixer.music.load(pathBGM + x)
                    pygame.mixer.music.play(-1)
                else:
                    pygame.mixer.music.fadeout(2000)
                    pygame.mixer.music.load(pathBGM + x)
                    pygame.mixer.music.play(-1)
            else:
                pygame.mixer.music.fadeout(2000)
                pygame.mixer.music.stop()
        else:
            if x != None:
                pygame.mixer.music.load(pathBGM + x)
                pygame.mixer.music.play(-1)
                pygame.mixer.music.pause()
            else:
                pygame.mixer.music.stop()
               
    #Mutes everything 
    def mute(self):
        self.muted = True
        
        if pygame.mixer.music.get_busy() == True:
            pygame.mixer.music.pause()
        
    #Unmutes everything
    def unmute(self):
        self.muted = False
        
        pygame.mixer.music.unpause()
        
    #Set Volume
    #Pass a number from 0.0 to 1.0
    #First argument is background volum, second is sound effect volume
    def volume(self, bV, sV):
        self.bgmVolume = bV
        self.sfxVolume = sV
        pygame.mixer.music.set_volume(bV)